package org.framework.funtionNode.manageParty;

import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.quickbundle.base.beans.factory.RmBeanFactory;
import org.quickbundle.base.web.page.RmPageVo;
import org.quickbundle.config.RmConfig;
import org.quickbundle.orgauth.IOrgauthConstants;
import org.quickbundle.orgauth.cache.RmAuthorizeCache;
import org.quickbundle.orgauth.custom.impl.RmCustomOrgService;
import org.quickbundle.orgauth.rmauthorize.vo.RmAuthorizeVo;
import org.quickbundle.orgauth.rmauthorizeresource.service.IRmAuthorizeResourceService;
import org.quickbundle.orgauth.rmauthorizeresource.vo.RmAuthorizeResourceVo;
import org.quickbundle.orgauth.rmfunctionnode.service.IRmFunctionNodeService;
import org.quickbundle.orgauth.rmfunctionnode.util.IRmFunctionNodeConstants;
import org.quickbundle.orgauth.rmfunctionnode.vo.RmFunctionNodeVo;
import org.quickbundle.orgauth.rmparty.service.IRmPartyService;
import org.quickbundle.orgauth.rmparty.util.IRmPartyConstants;
import org.quickbundle.orgauth.rmparty.vo.RmPartyVo;
import org.quickbundle.orgauth.rmpartyrelation.service.IRmPartyRelationService;
import org.quickbundle.orgauth.rmpartyrelation.service.impl.RmPartyRelationService;
import org.quickbundle.orgauth.rmpartyrelation.util.IRmPartyRelationConstants;
import org.quickbundle.orgauth.rmpartyrelation.vo.RmPartyRelationVo;
import org.quickbundle.orgauth.rmpartytype.service.impl.RmPartyTypeService;
import org.quickbundle.orgauth.rmpartytype.util.IRmPartyTypeConstants;
import org.quickbundle.orgauth.rmpartytype.vo.RmPartyTypeVo;
import org.quickbundle.orgauth.rmrole.service.IRmRoleService;
import org.quickbundle.orgauth.rmuser.service.IRmUserService;
import org.quickbundle.orgauth.rmuser.vo.RmUserVo;
import org.quickbundle.orgauth.util.impl.RmOrgService;
import org.quickbundle.project.IGlobalConstants;
import org.quickbundle.project.RmGlobalReference;
import org.quickbundle.project.RmProjectHelper;
import org.quickbundle.project.cache.RmSqlCountCache;
import org.quickbundle.project.common.vo.RmCommonVo;
import org.quickbundle.tools.helper.RmJspHelper;
import org.quickbundle.tools.helper.RmPopulateHelper;
import org.quickbundle.tools.helper.RmSqlHelper;
import org.quickbundle.tools.helper.RmStringHelper;
import org.quickbundle.tools.helper.RmVoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dfhc.ISystemConstant;
import com.dfhc.pub.concurrencycontrol.RequestConcurrentRoute;
import com.dfhc.pub.service.PickMenuLinkInPageService;
import com.dfhc.pub.service.PubParamService;
import com.dfhc.rm.user.IUserConstants;
import com.dfhc.rm.user.service.UserService;
import com.dfhc.rm.user.vo.UserVo;
import com.dfhc.util.CommentFileFilter;
import com.dfhc.util.FileOperateHelper;
import com.dfhc.util.HttpClientUtil;
import com.dfhc.util.JspHelper;
import com.dfhc.util.StringHelper;

/**
 * @author ASUS 会员列表(例子)
 */
@Controller
@RequestMapping(value = "/manegePartyTab")
public class ManegePartyTabController implements IGlobalConstants  {
	
	/*
	 * 註解
	 * 
	 * */
	
	private static final String PICK_MENU_SERVER_NAME = "localhost";
	@Autowired
	private IRmFunctionNodeService iRmFunctionNodeService;
	
	/*
	 * 查询list
	 * */
	@RequestMapping(value = "")
	public String treelist(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		String view_id = request.getParameter("view_id");
		String name="";
		List<RmCommonVo> lcvo = RmProjectHelper.getCommonServiceInstance().doQuery("select ID, NAME from RM_PARTY_VIEW");
		Map<String, String> mpv = new HashMap<String, String>();
		for(RmCommonVo cvo : lcvo) {
			mpv.put(cvo.getString("id"), cvo.getString("name"));
			if("".equals(view_id) || view_id ==null) {
				view_id = cvo.getString("id");
				name = cvo.getString("name");
			}
		}
		int i = 1 ;
		String sValue ="";
		String cmd="org";
		if(IOrgauthConstants.PartyView.DEFAULT.id().equals(view_id)){
			sValue = IOrgauthConstants.OrgTree.DEFAULT.value();
		}
		if (!"".equals(cmd)	) {
			cmd="org";
		}else {
			cmd=sValue;
		}
		dataMap.put("view_id", view_id);
		dataMap.put("cmd", cmd);
		dataMap.put("sValue", sValue);
		dataMap.put("name",name);
		request.setAttribute("list", dataMap);
		return "/qb5activiti/manegePartyTab/listManegePartyTab";
	}
	@RequestMapping(value = "getParty")
	public String list(Model model, HttpServletRequest request) {
		String view_id = request.getParameter("view_id");
		if (view_id == null || view_id.trim().length() == 0) {
			// view_id的默认值是RM_PARTY_VIEW表的第一条记录
			List<RmCommonVo> lPartyViewId = RmProjectHelper
					.getCommonServiceInstance().doQuery(
							"SELECT ID FROM RM_PARTY_VIEW ORDER BY ID", 1, 1);
			if (lPartyViewId.size() > 0) {
				view_id = lPartyViewId.get(0).getString("id");
			} else {
				view_id = IOrgauthConstants.PartyView.DEFAULT.id();
			}
		}

		// 优先获取parent_party_code
		String parent_party_code = request.getParameter("parent_party_code");

		// 如果parent_party_code为null，获取parent_party_id且默认为""
		String parent_party_id = null;
		if (parent_party_code == null) {
			parent_party_id = request.getParameter("parent_party_id");
			if (parent_party_id == null) {
				parent_party_id = "";
			}
		}

		// 是否包含自身，默认为0。只有懒加载时有效
		boolean include_self = false;
		include_self = "1".equals(request.getParameter("include_self"));

		// 获取show_bk
		String[] aShow_bk = new String[0];
		String show_bk = request.getParameter("show_bk");
		if (show_bk != null && show_bk.trim().length() > 0) {
			String tempBk = show_bk.replaceAll("^,+", "").replaceAll(",+$", "");
			if (tempBk.trim().length() > 0) {
				aShow_bk = tempBk.trim().split(",");
			}
		}

		// 获取lazy_init，默认为1
		String lazy_init = request.getParameter("lazy_init");
		if (lazy_init == null || lazy_init.length() == 0
				|| (!"1".equals(lazy_init) && !"0".equals(lazy_init))) {
			lazy_init = "0";
		}
		// 获取允许挂接用户 add by longsebo 2016-11-14
		String is_allow_hanging_user = "";
		if (!StringHelper
				.isEmpty(request.getParameter("is_allow_hanging_user"))) {
			is_allow_hanging_user = request
					.getParameter("is_allow_hanging_user");
		}
		// 获取允许挂接角色
		String is_allow_hanging_role = "";
		if (!StringHelper
				.isEmpty(request.getParameter("is_allow_hanging_role"))) {
			is_allow_hanging_role = request
					.getParameter("is_allow_hanging_role");
		}
		// 查询树节点的sql语句
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("select PR.PARENT_PARTY_ID, PR.CHILD_PARTY_ID, PR.PARENT_PARTY_CODE, PR.CHILD_PARTY_CODE, "
				+ "PR.CHILD_IS_LEAF, PR.CHILD_PARTY_NAME, P.PARTY_TYPE_ID as CHILD_PARTY_TYPE_ID, PT.ICON as CHILD_ICON, "
				+ "PT.BS_KEYWORD as CHILD_BS_KEYWORD, PR.CHILD_IS_MAIN_RELATION, PR.ORDER_CODE from RM_PARTY_RELATION PR "
				+ "join RM_PARTY P on PR.CHILD_PARTY_ID=P.ID "
				+ "join RM_PARTY_TYPE PT on P.PARTY_TYPE_ID=PT.ID where ");

		// 懒加载，只获取1层子节点
		if ("1".equals(lazy_init)) {
			// 如果parent_party_code未指定，按parent_party_id查询
			if (parent_party_code == null) {
				if ("".equals(parent_party_id)) {
					sbSql.append(" (PR.PARENT_PARTY_ID is null or PR.PARENT_PARTY_ID='')");
				} else {
					if (include_self) { // 查自身
						sbSql.append(" PR.CHILD_PARTY_ID=" + parent_party_id);
					} else { // 查父节点
						sbSql.append(" PR.PARENT_PARTY_ID=" + parent_party_id);
					}
				}
			} else { // parent_party_code不为空则按parent_party_code查询
				if ("".equals(parent_party_code)) {
					sbSql.append(" (PR.PARENT_PARTY_CODE is null or PR.PARENT_PARTY_CODE='')");
				} else {
					if (include_self) { // 查自身
						sbSql.append(" PR.CHILD_PARTY_CODE='"
								+ parent_party_code + "'");
					} else { // 查父节点
						sbSql.append(" PR.PARENT_PARTY_CODE='"
								+ parent_party_code + "'");
					}
				}
			}
			// 如果show_bk不为空，过滤
			if (aShow_bk.length > 0) {
				sbSql.append(" and PT.BS_KEYWORD in ("
						+ RmStringHelper.parseToSQLStringApos(aShow_bk) + ")");
			}
		} else {
			// 如果parent_party_code未指定，按parent_party_id查询
			if (parent_party_code == null) {
				StringBuilder sql_parentParty = new StringBuilder();
				sql_parentParty
						.append("select PR.CHILD_PARTY_CODE, PARENT_PARTY_CODE, CHILD_IS_MAIN_RELATION from RM_PARTY_RELATION PR where PR.PARTY_VIEW_ID="
								+ view_id);
				if ("".equals(parent_party_id)) {
					sql_parentParty
							.append(" and (PR.PARENT_PARTY_ID is null or PR.PARENT_PARTY_ID='')");
				} else {
					sql_parentParty.append(" and PR.PARENT_PARTY_ID="
							+ parent_party_id);
				}
				List<String> lParentPartyCode = RmProjectHelper
						.getCommonServiceInstance().query(
								sql_parentParty.toString(), new RowMapper() {
									public Object mapRow(ResultSet rs, int i)
											throws SQLException {
										return rs.getString("CHILD_PARTY_CODE");
									}
								});
				int index = 0;
				if (lParentPartyCode.size() > 0) {
					for (String parentPartyCode : lParentPartyCode) {
						if (index == 0) {
							sbSql.append(" (PR.CHILD_PARTY_CODE like '"
									+ parentPartyCode + "%'");
						} else {
							sbSql.append(" or PR.CHILD_PARTY_CODE like '"
									+ parentPartyCode + "%'");
						}
						index++;
					}
					sbSql.append(")");
				} else {
					sbSql.append(" PR.CHILD_PARTY_CODE like '" + NOT_EXIST_ID
							+ "%'");
				}
			} else {
				sbSql.append(" PR.CHILD_PARTY_CODE like '" + parent_party_code
						+ "%'");
			}
			// 如果show_bk不为空，过滤
			if (aShow_bk.length > 0) {
				sbSql.append(" and PT.BS_KEYWORD in ("
						+ RmStringHelper.parseToSQLStringApos(aShow_bk) + ")");
			}
		}
		// 增加是否允许挂接用户，角色条件 add by longsebo 2016-11-14
		if (!StringHelper.isEmpty(is_allow_hanging_user)) {
			sbSql.append("and PT.IS_ALLOW_HANGING_USER='")
					.append(is_allow_hanging_user).append("'");
		}
		if (!StringHelper.isEmpty(is_allow_hanging_role)) {
			sbSql.append("and PT.IS_ALLOW_HANGING_ROLE='")
					.append(is_allow_hanging_role).append("'");
		}
		sbSql.append(" and PR.PARTY_VIEW_ID=" + view_id);
		List<RmCommonVo> lcvo = RmProjectHelper.getCommonServiceInstance()
				.doQuery(sbSql.toString());
		model.addAttribute("beans", lcvo);
		model.addAttribute("last", lcvo.size());
		model.addAttribute("objKey", request.getParameter("objKey"));// form对应要回写的input的那么属性，第一个为要会写的id，
		model.addAttribute("fromName", request.getParameter("formName"));// from名称name属性
		model.addAttribute("view_id", view_id);// from名称name属性
		model.addAttribute("source", request.getParameter("source"));// 标志请求从哪儿来的，source=orgBack：说明是后台组织结构,
																		// source=assignDepts说明来自activiti人员分配
																		// source=orgFront说明来自前台选择组织机构
		if ("orgFront".equals(request.getParameter("source"))) {
			return "/qb5activiti/manegePartyTab/orgFront";
		} else if ("orgBack".equals(request.getParameter("source"))) {
			return "/qb5activiti/manegePartyTab/radioBack";
		} else if ("assignDepts".equals(request.getParameter("source"))) {
			return "/qb5activiti/manegePartyTab/assignDepts";
		} else if ("partymanage".equals(request.getParameter("source"))) {// 受控菜单组织树管理
			return "/qb5activiti/manegePartyTab/orgPartyTreeManage";
		} else {
			return "/qb5activiti/manegePartyTab/radio";
		}

	}
	/**
	 * 页面跳转
	 * @throws IOException 
	 */
	@RequestMapping(value = "/manageParty")
	public String manageParty(Model model, HttpServletRequest request ,HttpServletResponse response) throws IOException {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		String view_id=request.getParameter("view_id");
		String cmd=request.getParameter("cmd");
		//接收类型
		String fbb=request.getParameter("fbb");
		dataMap.put("view_id", view_id);
		request.setAttribute("dataMap", dataMap);
		if ("org".equals(cmd)) {
			String party_type = request.getParameter("party_type");
			System.out.print(party_type);
			if("relationViewRootNode".equals(fbb)) {
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				RmPopulateHelper.populate(prvo, request);
				prvo.setParty_view_id(view_id);
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				prService.insert(prvo);
				response.sendRedirect("manageParty.jsp?refresh_parent=1&view_id=" + view_id + "&child_party_code=" + request.getParameter("parent_party_code"));
			} else if("relationChildNode".equals(fbb)) {
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				RmPopulateHelper.populate(prvo, request);
				prvo.setParty_view_id(view_id);
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				prService.insert(prvo);
				response.sendRedirect("manageParty.jsp?refresh_parent=1&view_id=" + view_id + "&child_party_code=" + request.getParameter("parent_party_code"));
			} else if("addViewRootNode".equals(fbb)) {
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				String id = pService.insert(pvo);

				//团体关系
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				prvo.setParty_view_id(view_id);
				prvo.setChild_party_id(pvo.getId());
				prvo.setChild_party_name(pvo.getName());
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				prService.insert(prvo);
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Content-Type", "application/xml; charset=UTF-8");  
				RmJspHelper.goUrlWithAlert(request, response, "创建根节点成功", "/jsp/util/refreshParent.jsp");
			} else if("insertChildNode".equals(fbb)) {
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				String id = pService.insert(pvo);
				//团体关系
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				prvo.setParty_view_id(view_id);
				prvo.setParent_party_id(request.getParameter("parent_party_id"));
				prvo.setChild_party_id(pvo.getId());
				prvo.setChild_party_name(pvo.getName());
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				prService.insert(prvo);
				RmJspHelper.goUrlWithAlert(request, response, "新增子节点成功", "/jsp/util/refreshParent.jsp");
			} else if("deleteChildNode".equals(fbb)) {
				RmProjectHelper.getCommonServiceInstance().doUpdate(
					"delete from RM_PARTY_RELATION where CHILD_PARTY_CODE='" + request.getParameter("parent_party_code") + "' and party_view_id=" + request.getParameter("view_id"));
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				pService.delete(request.getParameter("parent_party_id"));		
				RmJspHelper.goUrlWithAlert(request, response, "删除本节点成功", "/jsp/util/refreshParent.jsp");
			} else if("deleteChildRelation".equals(fbb)) {
				RmProjectHelper.getCommonServiceInstance().doUpdateBatch(new String[]{
						"delete from RM_PARTY_RELATION where CHILD_PARTY_CODE='" + request.getParameter("parent_party_code") + "' and party_view_id=" + request.getParameter("view_id")
					});
					RmJspHelper.goUrlWithAlert(request, response, "删除本关系成功", "/jsp/util/refreshParent.jsp");
			} else if("updateOrderCode".equals(fbb)) {
				RmProjectHelper.getCommonServiceInstance().doUpdate("update RM_PARTY_RELATION SET ORDER_CODE=? WHERE CHILD_PARTY_CODE=?", 
						new String[]{request.getParameter("order_code"), request.getParameter("child_party_code")});
			} else if("updateThisPartyName".equals(fbb)) {
				String thisPartyName = request.getParameter("this_party_name");
				String thisPartyId = request.getParameter("parent_party_id");
				RmProjectHelper.getCommonServiceInstance().doUpdateBatch(new String[]{
						"update RM_PARTY_RELATION SET CHILD_PARTY_NAME='" + thisPartyName + "' WHERE CHILD_PARTY_ID=" + thisPartyId, 
						"update RM_PARTY_RELATION SET PARENT_PARTY_NAME='" + thisPartyName + "' WHERE PARENT_PARTY_ID=" + thisPartyId
				});
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				pvo.setId(thisPartyId);
				pvo.setName(thisPartyName);
				pService.update(pvo);
			}
			//当前的party
			RmCommonVo cvo = null;
			String child_party_code = request.getParameter("child_party_code");
			if(child_party_code == null) {
				child_party_code = "";
			} else {
				List<RmCommonVo> lcvo = RmProjectHelper.getCommonServiceInstance().doQuery("select * from RM_PARTY_RELATION where child_party_code='" + child_party_code + "'");
				if(lcvo.size() > 0) {
					cvo = lcvo.get(0);
				}
			}
			String ptCondition = null; 
			if(cvo != null){
				ptCondition = "BS_KEYWORD in(select c.BS_KEYWORD from RM_PARTY a " +
						"join RM_PARTY_TYPE_RELATION_RULE b on a.PARTY_TYPE_ID=b.PARENT_PARTY_TYPE_ID " +  
						"join RM_PARTY_TYPE c on b.CHILD_PARTY_TYPE_ID=c.id " + 
						"where b.PARTY_VIEW_ID=" + view_id + " and a.ID=" + cvo.getString("child_party_id") + ")";
			} else {
				ptCondition = "BS_KEYWORD in (select a.BS_KEYWORD from RM_PARTY_TYPE a join RM_PARTY_TYPE_RELATION_RULE b on a.ID=b.CHILD_PARTY_TYPE_ID " +
						" where b.PARENT_PARTY_TYPE_ID='' or b.PARENT_PARTY_TYPE_ID is null )";
			}
			//当前节点规则
			RmPartyTypeService partyTypeService = (RmPartyTypeService)RmBeanFactory.getBean(IRmPartyTypeConstants.SERVICE_KEY);
			List<RmPartyTypeVo> lAvailablePartyType = partyTypeService.queryByCondition(ptCondition, null);
			model.addAttribute("view_id", view_id);
			model.addAttribute("child_party_code", child_party_code);
			model.addAttribute("rmPartyTypeVo", lAvailablePartyType);
			model.addAttribute( "bean", cvo);
			return "/qb5activiti/manegePartyTab/manageParty";
		}else {
			return "/qb5activiti/manegePartyTab/tmParty";
		}
	}
	/**
	 * 页面跳转
	 * @throws IOException 
	 */
	@RequestMapping(value = "/managePartylist")
	@ResponseBody
	public Map<String , Object> managePartylist(Model model, HttpServletRequest request ,HttpServletResponse response) throws IOException {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		String view_id=request.getParameter("view_id");
		String cmd=request.getParameter("cmd");
		//接收类型
		String fbb=request.getParameter("fbb");
		dataMap.put("view_id", view_id);
		request.setAttribute("dataMap", dataMap);
		if ("org".equals(cmd)) {
			String party_type = request.getParameter("party_type");
			System.out.print(party_type);
			if("relationViewRootNode".equals(fbb)) {
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				RmPopulateHelper.populate(prvo, request);
				prvo.setParty_view_id(view_id);
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				try{
				prService.insert(prvo);
				model.addAttribute("view_id", view_id);
				dataMap.put("", request.getParameter("parent_party_code"));
				dataMap.put("refresh_parent", 1);
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "增加 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "添加 失败!");	
			}
				//response.sendRedirect("manageParty.jsp?refresh_parent=1&view_id=" + view_id + "&child_party_code=" + request.getParameter("parent_party_code"));
			} else if("relationChildNode".equals(fbb)) {
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				RmPopulateHelper.populate(prvo, request);
				prvo.setParty_view_id(view_id);
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				try{
				prService.insert(prvo);
				dataMap.put("view_id", view_id);
				dataMap.put("", request.getParameter("parent_party_code"));
				dataMap.put("refresh_parent", 1);
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "增加 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "添加 失败!");	
			}
//				response.sendRedirect("manageParty.jsp?refresh_parent=1&view_id=" + view_id + "&child_party_code=" + request.getParameter("parent_party_code"));
			} else if("addViewRootNode".equals(fbb)) {
				try {
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
//				
//				pvo.setParty_type_id(view_id);
//				pvo.setOld_party_id(pvo.getId());
//				//pvo.setParty_type_id_name(partyTypeIdName);(pvo.getName());
				String id = pService.insert(pvo);

				//团体关系
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				prvo.setParty_view_id(view_id);
				prvo.setChild_party_id(pvo.getId());
				prvo.setChild_party_name(pvo.getName());
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
					prService.insert(prvo);
					dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
					dataMap.put(ISystemConstant.AJAX_MESSAGE, "增加 成功!" );
				} catch (Exception e) {
					dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
					dataMap.put(ISystemConstant.AJAX_MESSAGE, "添加 失败!");	
				}
			} else if("insertChildNode".equals(fbb)) {
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				String id = pService.insert(pvo);
				//团体关系
				RmPartyRelationVo prvo = new RmPartyRelationVo();
				prvo.setParty_view_id(view_id);
				prvo.setParent_party_id(request.getParameter("parent_party_id"));
				prvo.setChild_party_id(pvo.getId());
				prvo.setChild_party_name(pvo.getName());
				IRmPartyRelationService prService = (IRmPartyRelationService)RmBeanFactory.getBean(IRmPartyRelationConstants.SERVICE_KEY);
				try{
				prService.insert(prvo);
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "增加 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "添加 失败!");	
			}
			} else if("deleteChildNode".equals(fbb)) {
				RmProjectHelper.getCommonServiceInstance().doUpdate(
					"delete from RM_PARTY_RELATION where CHILD_PARTY_CODE='" + request.getParameter("parent_party_code") + "' and party_view_id=" + request.getParameter("view_id"));
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				try{
				pService.delete(request.getParameter("parent_party_id"));	
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "更新 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "更新 失败!");	
			}
			} else if("deleteChildRelation".equals(fbb)) {
				try{
				RmProjectHelper.getCommonServiceInstance().doUpdateBatch(new String[]{
						"delete from RM_PARTY_RELATION where CHILD_PARTY_CODE='" + request.getParameter("parent_party_code") + "' and party_view_id=" + request.getParameter("view_id")
					});
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "删除 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "shanch 失败!");	
			}
			} else if("updateOrderCode".equals(fbb)) {
				RmProjectHelper.getCommonServiceInstance().doUpdate("update RM_PARTY_RELATION SET ORDER_CODE=? WHERE CHILD_PARTY_CODE=?", 
						new String[]{request.getParameter("order_code"), request.getParameter("child_party_code")});
			} else if("updateThisPartyName".equals(fbb)) {
				String thisPartyName = request.getParameter("this_party_name");
				String thisPartyId = request.getParameter("parent_party_id");
				RmProjectHelper.getCommonServiceInstance().doUpdateBatch(new String[]{
						"update RM_PARTY_RELATION SET CHILD_PARTY_NAME='" + thisPartyName + "' WHERE CHILD_PARTY_ID=" + thisPartyId, 
						"update RM_PARTY_RELATION SET PARENT_PARTY_NAME='" + thisPartyName + "' WHERE PARENT_PARTY_ID=" + thisPartyId
				});
				IRmPartyService pService = (IRmPartyService)RmBeanFactory.getBean(IRmPartyConstants.SERVICE_KEY);
				//团体
				RmPartyVo pvo = new RmPartyVo();
				RmPopulateHelper.populate(pvo, request);
				pvo.setId(thisPartyId);
				pvo.setName(thisPartyName);
				try{
				pService.update(pvo);
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "更新 成功!" );
			} catch (Exception e) {
				dataMap.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
				dataMap.put(ISystemConstant.AJAX_MESSAGE, "更新 失败!");	
			}
			}
			//当前的party
			RmCommonVo cvo = null;
			String child_party_code = request.getParameter("child_party_code");
			if(child_party_code == null) {
				child_party_code = "";
			} else {
				List<RmCommonVo> lcvo = RmProjectHelper.getCommonServiceInstance().doQuery("select * from RM_PARTY_RELATION where child_party_code='" + child_party_code + "'");
				if(lcvo.size() > 0) {
					cvo = lcvo.get(0);
				}
			}
			String ptCondition = null; 
			if(cvo != null){
				ptCondition = "BS_KEYWORD in(select c.BS_KEYWORD from RM_PARTY a " +
						"join RM_PARTY_TYPE_RELATION_RULE b on a.PARTY_TYPE_ID=b.PARENT_PARTY_TYPE_ID " +  
						"join RM_PARTY_TYPE c on b.CHILD_PARTY_TYPE_ID=c.id " + 
						"where b.PARTY_VIEW_ID=" + view_id + " and a.ID=" + cvo.getString("child_party_id") + ")";
			} else {
				ptCondition = "BS_KEYWORD in (select a.BS_KEYWORD from RM_PARTY_TYPE a join RM_PARTY_TYPE_RELATION_RULE b on a.ID=b.CHILD_PARTY_TYPE_ID " +
						" where b.PARENT_PARTY_TYPE_ID='' or b.PARENT_PARTY_TYPE_ID is null )";
			}
			//当前节点规则
			RmPartyTypeService partyTypeService = (RmPartyTypeService)RmBeanFactory.getBean(IRmPartyTypeConstants.SERVICE_KEY);
			List<RmPartyTypeVo> lAvailablePartyType = partyTypeService.queryByCondition(ptCondition, null);
			request.setAttribute("rmPartyTypeVo", lAvailablePartyType);
			request.setAttribute( "bean", cvo);
			return dataMap	;
		}else {
			return dataMap;
		}
	}
}
